/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cl.jsoftlabs.gwt.soccer.server.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import cl.jsoftlabs.gwt.soccer.client.bean.PaisVO;
import cl.jsoftlabs.gwt.soccer.client.services.PaisService;
import cl.jsoftlabs.gwt.soccer.server.persistence.entities.Pais;
import cl.jsoftlabs.gwt.soccer.server.persistence.entities.UtilEntities;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class PaisServiceImpl extends RemoteServiceServlet implements PaisService {

	private static final long serialVersionUID = 4972479068047628178L;

	//@PersistenceContext(unitName = "TaskBoard-ejbPU")  
	@PersistenceContext
	EntityManager em;
	
	@Resource private UserTransaction utx;
	
	@Override
	public List<PaisVO> obtenerPaises() {
		@SuppressWarnings("unchecked")
		List<Pais> items  = em.createQuery("Select pais from Pais pais WHERE pais.booCodVisible = 1 ORDER BY pais.nombrePais").getResultList();
		
		List<PaisVO> vos = new ArrayList<PaisVO>();
		
		for (Iterator<Pais> iterator = items.iterator(); iterator.hasNext();) {
			Pais p = iterator.next();
			vos.add(UtilEntities.toVO(p));
		}
		
		return vos;
	}

	@Override
	public int actualizarRegistro(PaisVO vo) {
		Pais e = UtilEntities.toEntity(vo);
		try {
			utx.begin();
			em.merge(e);
			utx.commit();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

			System.out.println("Modifica Registro "+e);
			

		 
		return 0;
	}
	
	
	
	
}
